//
//  CollectTool.m
//  行记
//
//  Created by dllo on 15/10/8.
//  Copyright (c) 2015年 lhch. All rights reserved.
//

#import "CollectTool.h"
#import "DataBaseHandle.h"
#import "YZLCollectionModel.h"
@interface CollectTool ()

//@property (nonatomic, assign) int a;
@property (nonatomic, retain)  UIAlertView *alert;
@property (nonatomic, retain) UIAlertView *okAlert;

@end

@implementation CollectTool

#pragma mark - 设置单例
+ (instancetype)shareCollectTool {
    static CollectTool *collecTool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        collecTool = [[CollectTool alloc] init];
        
    });
    return collecTool;
}

#pragma mark - 取出数据库内容
+ (NSArray *)collectOpenDataBaseWithTable:(NSString *)table {
    NSMutableArray *modelArray = [NSMutableArray array];
    [[DataBaseHandle shareDataBaseHandle] openSql];
    [[DataBaseHandle shareDataBaseHandle] createTable:table];
    NSArray *array = [[DataBaseHandle shareDataBaseHandle] selectStudentWithTable:table];
    for (YZLCollectionModel *model in array) {
        [modelArray addObject:model];
    }
    [[DataBaseHandle shareDataBaseHandle] closeDB];
    NSArray *arr = modelArray;
    return arr;
}

#pragma mark - 判断数据库中是否存在 nid
+ (BOOL)dataBaseIncludeWithTitle:(NSString *)Title withNid:(NSString *)nid {
    [[DataBaseHandle shareDataBaseHandle] openSql];
    [[DataBaseHandle shareDataBaseHandle] createTable:Title];
    NSArray *array = [[DataBaseHandle shareDataBaseHandle] selectStudentWithTable:Title];
    int a = 0;
    for (YZLCollectionModel *model in array) {
        nid = [NSString stringWithFormat:@"%@",nid];
        if ([model.nID isEqualToString:nid]) {
            a = 1;
        }
    }
    [[DataBaseHandle shareDataBaseHandle] closeDB];
    if (a == 1) {
        return YES;
    } else return NO;
}

#pragma mark - 改变 button 的状态
+ (void)changeWithButton:(UIBarButtonItem *)button WithTempBool:(BOOL )tempBool {
    if (tempBool == YES) {
        [button setImage:[UIImage imageNamed:@"noshoucang"]];
    } else {
        [button setImage:[UIImage imageNamed:@"shoucang"]];
    }
}

#pragma mark - 插入或删除收藏
- (void)isCollectWithTitle:(NSString *)titel withtempBool:(BOOL )tempBool withNid:(NSString *)nid WithButton:(UIBarButtonItem *)button WithName:(NSString *)name WithImage:(NSString *)image {
    [[DataBaseHandle shareDataBaseHandle] openSql]; /**< 开启数据库 */
    [[DataBaseHandle shareDataBaseHandle] createTable:titel]; /**< 创建数据库列表 */
    
    YZLCollectionModel *model = [[YZLCollectionModel alloc] init];

    /* 判断是否已经收藏 */
    if (YES == tempBool) {
   /* 如果已经收藏，则取消收藏，删除数据库内容 */
        [[DataBaseHandle shareDataBaseHandle] deleteWithNid:nid withTable:titel];
        
        [button setImage:[UIImage imageNamed:@"shoucang"]];
        
        self.alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"取消收藏" delegate: nil cancelButtonTitle:nil otherButtonTitles: nil, nil];
        
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(dismissAlertView:) userInfo:nil repeats:NO];
        [self.alert show];
        
        /* 将收藏状态变为未收藏 */
        tempBool = NO;
        
    } else {
       /* 如果为收藏，则收藏，插入数据库内容 */
        
        [button setImage:[UIImage imageNamed:@"noshoucang"]];
        
        model.nID = nid;
        model.title = name;
        model.image = image;
        
        [[DataBaseHandle shareDataBaseHandle] insertWithCollect:model withTable:titel];
        self.okAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate: nil cancelButtonTitle:nil otherButtonTitles: nil, nil];
        
        [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(dismissAlertView:) userInfo:nil repeats:NO];
        
        [self.okAlert show];
        
        /* 将收藏状态变为收藏 */
        tempBool = YES;
        
    }
   
    /* 关闭数据库 */
    [[DataBaseHandle shareDataBaseHandle] closeDB];
}

- (void)dismissAlertView:(NSTimer *)timer {
    [self.alert dismissWithClickedButtonIndex:5 animated:YES];
    [self.okAlert dismissWithClickedButtonIndex:5 animated:YES];
}

@end
